package com.yanqu.road.server.cmd.crossunion;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.union.RechargeUserInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;
import java.util.Map;

@Cmd(code = CrossProtocol.C_CROSS_UNION_RECHARGE_REWARD,desc = "")
public class CrossUnionRechargeGetRewardCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUnionActivityRankDataProto.CrossUnionRechargeGetRewardMsg reqMsg = CrossUnionActivityRankDataProto.CrossUnionRechargeGetRewardMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        String unionId = reqMsg.getUnionId();
        int conditionId = reqMsg.getConditionId();
        long userId = packet.getPlayerId();
        if (reqMsg.hasUserBaseInfo()) {
            PlayerProto.PlayerBaseTempMsg userBaseInfo = reqMsg.getUserBaseInfo();
            UserBaseInfo baseInfoa = PlayerBasePb.parseBaseUserInfo(userBaseInfo);
            CrossUserMgr.updateUserBaseInfo(userId, baseInfoa);
        }
        int getRewardCount = 0;
        UnionActivityConditionData conditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 0, unionId);
        CrossUnionActivityRankDataProto.CrossUnionRechargeGetRewardRespMsg.Builder resp = CrossUnionActivityRankDataProto.CrossUnionRechargeGetRewardRespMsg.newBuilder();
        if (conditionData == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
        }else {

            Map<Integer, List<RechargeUserInfo>> infoMap = JSON.parseObject(conditionData.getInfo(), new TypeReference<Map<Integer, List<RechargeUserInfo>>>(){});
            List<RechargeUserInfo> userRechargeList = infoMap.get(conditionId);
            if(null == userRechargeList){
                ret = GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE;
            }else {
                for (RechargeUserInfo info : userRechargeList) {
                    if (info.isGetReward()) {
                        getRewardCount++;
                        if (info.getUserId() == userId) {
                            ret = GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
                        }
                    }
                }
            }
        }
        resp.setRet(ret);
        if (ret == 0) {
            resp.setActivityId(activityId);
            resp.setUnionId(unionId);
            resp.setRewardCount(getRewardCount);
            resp.setConditionId(conditionId);
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_SYNC_CROSS_UNION_RECHARGE_REWARD, resp);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
